package com.fitness.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class CommonUtil {
	
	private static final Logger LOG = LoggerFactory.getLogger(CommonUtil.class);

//短信验证的错误码
	/**超时*/
	public final static int SMS_TIMEOUT=-10000;
	/**成功*/
	public final static int SMS_SUCCESS=10000;
	/**找不到*/
	public final static int SMS_FINDNONE=10001;
//异常提示的错误码
	/**成功*/
	public final static int ERROR_SUCCESS = 200;//成功
    /**无返回类型*/	
	public final static int ERROR_NONE=400;//无返回类型
	/**代码处理异常*/
	public final static int ERROR_HANDLE=500;//代码处理异常
	/**物理类型错误*/
	public final static int ERROR_CONTECTOUT = 601;//物理类型错误
//排序使用的提示码
	/**按照时间排序*/
	public final static int SORT_TIME = 0;
	/**按照城市排序*/
	public final static int SORT_CITY = 1;
	/**按照热门推荐排序*/
	public final static int SORT_HOT = 2;
	/**
	 * 支付宝
	 */
	public final static String APP_PRIVATE_KEY="MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQC+JYmKUhMYYEZaI8+ruCRCo7foS4bx85QrIAIC1A/AcYuBMj8vfyidikcDLfYJll2oGMStLg0+mrlYcd5lWi8pwOVT+7R7vG73sF8bCl/R1H2yft7ii6DZpgZLxjaYSYbCBFgDdQfEzpF1OIBSyZnXxHoL0ZmcxiqzO3z4bfmVsgmfS8nwb0JH7phnym9xZkWAL8cxNyE7t2/kjDx9066KQ4AvQ0EDMIOfUwhlHGiwQ9uZEi/jdMl3iYfuk9l7ACVFGO9lUeOjf0j58oNvHNYZ14ILNcDPWsHrwMS6HoyXmm7mwxKCpzrRrn0OWzkxXIHFN09p8oasZRns6m9m/I6LAgMBAAECggEAdxGsejiBCp+nDmUkc0ShODWrQUSiYTqFiyfE+p5uqrSvhhy/4m8/ML/ZTg1m54QIa+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kPMDKPi9QKNQYMHs9grrzh7bKpc18bjDOPuhM3yq+0g0DbRc5CPYmDUuQVz27MCszSwqrd90xVKU";
	public final static String APPID="2017073107972187";
	public final static String ALIPAYPULICKEY="MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAkxSx60HBcKVkDn97rv77fFjEl9MNZDZu8wOr5ew2QRanLjo6XT/6yCMhcwMdrbN0PYpEJbh6VNB/c1sHXMLL3czVXYwmKJGPu5bgviUidyOSFXcjkQMGdOzL6lrhOtlQR6WdL5Hu6re/fOdynCJ27UKFQRXk9HLllonghD96Rx9oGoFroDo0AeKXRpOLO4+iUfrU8JBkCJ6TQRcGWn7VI/ixM9cc0YIwycOBdr7+IVJfcpGmPSfW/VSr/olKeWwJD7yVxpcR/bF/9Kyzus07GNS2j6+S4fl2QtWGHUyMZGwm+WjPe5wB27FMizefgKQbNCuhfYTPFJrIXuIv5mR5uQIDAQAB";
	public static String loginName(){
		String uuid =  UUID.randomUUID().toString();
		String random =  uuid.substring(0, 8);
		String chars = "abcdefghijklmnopqrstuvwxyz";
		char salt =  chars.charAt((int)(Math.random() * 26));
		return salt+random;
	}


	// 获取请求中所有的参数 map
	public static Map<String, String> requestParams(HttpServletRequest request){

		Map<String,String> params = new HashMap<String,String>();
		Map requestParams = request.getParameterMap();
		for(Iterator iter =requestParams.keySet().iterator();iter.hasNext();){
			String name = (String) iter.next();
			String[] values = (String[]) requestParams.get(name);
			String valueStr= "";
			for(int i=0;i<values.length;i++){
				valueStr=(i==values.length-1)?valueStr +values[i]:valueStr +values[i]+",";
			}
			//解决乱码问题
			//valueStr = new String(valueStr.getBytes("iso-8859-1"),"utf-8");
			params.put(name,valueStr);
		}
		return params;
	}
	
	/**
	 * 把request请求中的流写成字符串
	 * @param request
	 * @return
	 */
	public static String inputToString(HttpServletRequest request){
		
			StringBuilder sb = new StringBuilder();
		  try {
			InputStreamReader is = new InputStreamReader(request.getInputStream());
			BufferedReader reader = new BufferedReader(is);
			String line = null;     
            while ((line = reader.readLine()) != null) {   
                 sb.append(line);     
             }  
             is.close();   
             LOG.error("xml info :"+sb.toString());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		  return sb.toString();	
		  
	}
	
	public static String getIpAddr(HttpServletRequest request) {  
        String ip = request.getHeader("x-forwarded-for");  
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
            ip = request.getHeader("Proxy-Client-IP");  
        }  
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
            ip = request.getHeader("WL-Proxy-Client-IP");  
        }  
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
            ip = request.getRemoteAddr();  
        }  
        return ip;  
    } 
}
